package com.lgj.auth.jdbc;

import com.lgj.core.LoginUser;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author guangjie.liao
 * @Date: 2022/10/25 08:45
 * @Description:
 */
@Repository
public class UserRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    private static String SELECT_FALL_ROUTE_SQL = "select * from sys_user where  username = ";

    public LoginUser getLoginUser(String username){
        LoginUser user = jdbcTemplate.queryForObject(SELECT_FALL_ROUTE_SQL +"'"+ username + "'", new RowMapper<LoginUser>() {
            @Override
            public LoginUser mapRow(ResultSet rs, int rowNum) throws SQLException {
                LoginUser user = new LoginUser();
                user.setUserId(rs.getLong("id"));
                user.setUsername(rs.getString("username"));
                user.setAccountNonExpired(true);
                user.setAccountNonLocked(true);
                user.setCredentialsNonExpired(true);
                user.setEnabled(true);
                user.setPassword(rs.getString("password"));
                user.setAuthorities(new ArrayList<>());
                return user;
            }
        });
        return user;
    }
}
